<template>
  <div class="com-container">
    <div class="title" :style="fontSize">
      <span>‖ {{ title }}</span>
      <span class="iconfont title-icon" :style="fontSize" @click="showLists"
        >&#xe6eb;</span
      >
      <div
        class="select-list"
        v-show="showList"
        :style="fontSize"
      >
        <div
          class="select-li"
          v-for="item in selectedTypes"
          :key="item.key"
          @click="changeType(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { getThemeValue } from '../utils/theme_utils';
export default {
  data() {
    return {
      chartInstance: null,
      allData: [],
      showList: false,
      currentType: 'map',
      titleFontsize: 0,
    };
  },

  created() {
    window.addEventListener('resize', this.screenAdapter);
    // 注册回调函数
    this.$socket.registerCallBack('trendData', this.getData);
  },

  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend',
      value: '',
    });
    this.screenAdapter();
  },

  beforeDestroy() {
    window.removeEventListener('resize', this.screenAdapter);

    this.$socket.unRegisterCallBack('trendData');
  },

  computed: {
    selectedTypes() {
      if (!this.allData) {
        return [];
      } else {
        return this.allData.type?.filter(
          (item) => item.key !== this.currentType
        );
      }
    },
    title() {
      if (!this.allData) {
        return '';
      } else {
        return this.allData[this.currentType].title;
      }
    },
    fontSize() {
      return {
        fontSize: this.titleFontsize + 'px',
        color: getThemeValue(this.theme)?.titleColor
      };
    },
    marginL() {
      return {
        marginLeft: this.titleFontsize + 'px',
      };
    },
    ...mapState(['theme'])
  },

  methods: {
    // init chart
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs['trend_ref'], this.theme);
      // handle fixed options
      const initOption = {
        xAxis: {
          type: 'category',
          boundaryGap: false,
        },
        yAxis: {
          type: 'value',
        },
        tooltip: {
          trigger: 'axis',
        },
        grid: {
          left: '3%',
          top: '35%',
          right: '4%',
          bottom: '1%',
          containLabel: true,
        },
        legend: {
          left: 20,
          top: '15%',
          icon: 'circle',
        },
      };
      this.chartInstance.setOption(initOption);
    },

    // async getData() {
    //   const { data } = await this.$http.get('trend');
    //   console.log(data);
    //   this.allData = data;
    //   this.updateChart();
    // },
    async getData(data) {
      this.allData = data;
      this.updateChart();
    },

    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)',
      ];
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)',
      ];
      // handle data
      const timeArr = this.allData.common.month;
      // yAxios data => series
      const valueArr = this.allData[this.currentType].data;
      const seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: 'line',
          data: item.data,
          stack: this.currentType,
          // 填充风格
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              },
              {
                offset: 1,
                color: colorArr2[index],
              },
            ]),
          },
        };
      });
      const legendArr = valueArr.map((item) => item.name);
      // handle data options
      const dataOption = {
        xAxis: {
          data: timeArr,
        },
        series: seriesArr,
        legend: {
          data: legendArr,
        },
      };
      this.chartInstance.setOption(dataOption);
    },

    screenAdapter() {
      this.titleFontsize = (this.$refs['trend_ref'].offsetWidth / 100) * 3.6;
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontsize,
          itemHeight: this.titleFontsize,
          itemGap: this.titleFontsize,
          textStyle: {
            fontSize: this.titleFontsize / 2,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },

    showLists() {
      this.showList = !this.showList;
    },

    changeType(type) {
      console.log(type);
      this.currentType = type;
      this.updateChart();
      this.showList = false;
    },
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updateChart() // 更新图表的展示
    }
  }
};
</script>

<style scoped lang="scss">
.title {
  position: absolute;
  z-index: 10;
  color: #fff;
  top: 20px;
  left: 20px;

  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  // .select-list {
  //   background-color: #222733;
  // }
}
</style>
